package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.base.UserAccount;
import com.blueocean_health.care.management.domain.dto.NurseGroupDto;
import com.blueocean_health.care.management.domain.dto.SelectNurseGroupDto;
import com.blueocean_health.care.management.domain.groupvo.SelectNurseGroupVo;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.exception.OrderException;
import com.blueocean_health.care.management.service.UserService;
import com.blueocean_health.care.management.transfer.client.NurseGroupService;

/**
 * @Title: TestClientService.java
 * @Package com.blueocean_health.care.management.transfer.client
 * @author lcx
 * @date 2018年7月3日
 * @version V1.0
 */
@RestController
@RequestMapping(value = "/nurse/group", produces = MediaType.APPLICATION_JSON_VALUE)
public class NurseGroupController {
	@Autowired
	private NurseGroupService nurseGroupService;
	@Autowired
	private UserService userService;

	/**
	 * 小组下拉列表 2018年5月16日17:57:49
	 * 
	 * @return
	 */
	@RequestMapping("/pull")
	public SysResult pull(@RequestBody String jsonStr) {
		JSONObject jsonObject =  JSONObject.parseObject(jsonStr);
		if(!jsonObject.containsKey("teamId")) {
			return SysResult.error("参数异常");
		}
		return nurseGroupService.pull(jsonStr);
	}

	/**
	 * 查询小组
	 * 
	 * @return
	 */
	@RequestMapping("/query")
	public SysResult query(@RequestBody SelectNurseGroupDto selectNurseGroupDto) {
		User currentUserInfo = getCurrentUserInfo();
		JSONObject jo = new JSONObject();
		jo.put("selectNurseGroupDto", selectNurseGroupDto);
		jo.put("currentUserInfo", currentUserInfo);
		return nurseGroupService.query(jo.toJSONString());
	}

	/**
	 * 小组列表
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public SysResult list(@RequestBody SelectNurseGroupDto selectNurseGroupDto) {
		User currentUserInfo = getCurrentUserInfo();
		JSONObject jo = new JSONObject();
		jo.put("selectNurseGroupDto", selectNurseGroupDto);
		jo.put("currentUserInfo", currentUserInfo);
		return nurseGroupService.list(jo.toJSONString());
	}

	/**
	 * 导出Excel
	 * 
	 * @return
	 */
	@RequestMapping("/list-excel")
	public void exportListExcel(SelectNurseGroupDto selectNurseGroupDto, HttpServletResponse response) {
		User currentUserInfo = getCurrentUserInfo();
		JSONObject jo = new JSONObject();
		jo.put("selectNurseGroupDto", selectNurseGroupDto);
		jo.put("currentUserInfo", currentUserInfo);
		SysResult sysResult = nurseGroupService.listExcel(jo.toJSONString());
		try {
			List<SelectNurseGroupVo> list = (List<SelectNurseGroupVo>) sysResult.getData();

			String finalFileName = URLEncoder.encode("小组管理" + TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS) + ".xls",
					"UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			ExcelUtil.getInstance().exportObj2Excel(toClient, list, SelectNurseGroupVo.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 选择成员
	 * 
	 * @return
	 */
	@RequestMapping("/nurse/select-nurse")
	public SysResult selectNurse(SelectNurseGroupDto selectNurseGroupDto) {
		User currentUserInfo = getCurrentUserInfo();
		JSONObject jo = new JSONObject();
		jo.put("selectNurseGroupDto", selectNurseGroupDto);
		jo.put("currentUserInfo", currentUserInfo);
		return nurseGroupService.selectNurse(jo.toJSONString());
	}

	/**
	 * 模糊查询小组
	 * 
	 * @return
	 */
	@RequestMapping("/select-group")
	public SysResult selectGroup(@RequestBody(required = false) SelectNurseGroupDto selectNurseGroupDto) {
		User currentUserInfo = getCurrentUserInfo();
		JSONObject jo = new JSONObject();
		jo.put("selectNurseGroupDto", selectNurseGroupDto);
		jo.put("currentUserInfo", currentUserInfo);

		return nurseGroupService.selectGroup(jo.toJSONString());
	}

	// @RequestMapping("/add")
	// public SysResult insertNurseGroup(@RequestBody NurseGroupDto nurseGroupDto)
	// throws OrderException {
	// User currentUserInfo = getCurrentUserInfo();
	// JSONObject jo = new JSONObject();
	// jo.put("nurseGroupDto", nurseGroupDto);
	// jo.put("currentUserInfo",currentUserInfo);
	//
	// return nurseGroupService.insertNurseGroup(jo.toJSONString());
	// }
	//
	// @RequestMapping("/delete")
	// public SysResult deleteNurseGroup(@RequestBody String jsonString) throws
	// OrderException {
	// return nurseGroupService.deleteNurseGroup(jsonString);
	// }

	private User getCurrentUserInfo() {
		UserAccount userAccount = LocalManager.getVal(LocalManagerType.USER_ADMIN);
		User currentUserInfo = userService.getUserByUserId(userAccount.getUserId().toString());
		return currentUserInfo;
	}

}
